package com.minshenglife.zanzan.web.weixin;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.entity.Betting;
import com.minshenglife.zanzan.entity.OperateLog;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.pub.Config;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.result.RedFlowerResult;
import com.minshenglife.zanzan.service.BettingService;
import com.minshenglife.zanzan.service.IntegralService;
import com.minshenglife.zanzan.service.OperateLogService;
import com.minshenglife.zanzan.service.RedFlowerService;
import com.minshenglife.zanzan.service.UserService;
import com.minshenglife.zanzan.web.weixin.form.StatForm;
import com.nationsky.pub.utils.StringUtils;


/**
 * 押宝-花王-花落谁家
 * @author huodd
 * 
 */
@Controller
@RequestMapping("/guessKing")
public class GuessKingController extends BaseController {
	Logger log = LoggerFactory.getLogger(GuessKingController.class);
	@Resource
	IntegralService integralService;
	@Resource
	RedFlowerService redFlowerService;
	@Resource
	UserService userService;
	@Resource
	BettingService bittingService;
	@Resource
	OperateLogService logService;
    
    
    /**
     * searchYesTodayFlowerRank
     * @param request
     * @param response
     * @param statForm
     * @return
     */
    @RequestMapping(value="searchYesTodayFlowerRank.do",method=RequestMethod.POST)
    @ResponseBody
    public Message searchYesTodayFlowerRank(HttpServletRequest request, HttpServletResponse response,StatForm statForm){
    	Message message=null;
    	String operator=super.getEmpId(request);
		log.debug("当前操作人："+operator);
		try {
			int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
			int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
			log.debug("搜索的内容："+statForm.getContent());
			Page<RedFlowerResult> page=null;
			String msg="";
			if(StringUtils.isNullOrEmpty(statForm.getContent())){
				page=redFlowerService.findYesterdayFlowerRank(null, pageNo, pageSize);
				
				//是否可以押宝，1、每天上午9:00—11:00  2、1次机会
	    		message=isCanBetting(operator);
	    		if(Message.STATE_200.equals(message.getState())){
	    			msg="true";//可以进行押宝
	    		}
			}else{
				page=redFlowerService.findYesterdayFlowerListByCon(statForm.getContent(), pageNo, pageSize);
			}
			message=new Message(Message.STATE_200,msg,page);
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
		}
		
		return message;
    }
    /**
     * 花王-押宝
     * @param request
     * @param response
     * @param statForm
     * @return
     */
    @RequestMapping(value="bettingOfRedFlower.do",method=RequestMethod.POST)
    @ResponseBody
    public Message bettingOfRedFlower(HttpServletRequest request, HttpServletResponse response,StatForm statForm){
    	Message message=null;
    	OperateLog opLog=null;
    	String con="";
    	String operator=super.getEmpId(request);
		log.debug("当前操作人："+operator);
    	try {
			String empId=statForm.getEmpId();
			log.debug("被押宝人："+empId);
			if(StringUtils.isNullOrEmpty(operator) || StringUtils.isNullOrEmpty(empId)){
				con="非法操作，请回退";
				message=new Message(Message.STATE_one,con);
				
			}else{
	    		//是否可以押宝，1、每天上午9:00—11:00  2、1次机会
	    		message=isCanBetting(operator);
	    		if(Message.STATE_200.equals(message.getState())){
	    			//被押中的员工是否存在
	    			User user=userService.getUserInfo(empId);
	    			if(user==null){
	    				con="该员工不存在，请确认";
	    				message=new Message(Message.STATE_one,con);
	    			}else{
	    				//添加押宝流水
	    				bittingService.add(new Betting(operator,empId,Keys.BETTING_ISWIN_ZERO,Keys.BETTING_STATE_ZERO));
	    				
	    				con=operator+"猜"+empId+"是花王";
	    				message=new Message(Message.STATE_200);
	    			}
	    		}else{
	    			con=message.getMsg();
	    		}
			}
			//添加操作日志
			opLog=new OperateLog(operator,Keys.OPERATE_LOG_TYPE_EIGHT,Keys.OPERATE_LOG_STATE_SUCCESS,con);
		} catch (Exception e) {
			e.printStackTrace();
			opLog=new OperateLog(operator,Keys.OPERATE_LOG_TYPE_EIGHT,Keys.OPERATE_LOG_STATE_EXCEPTION,"服务器异常");
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug("服务器异常");
		}finally{
	    	//插入操作日志信息
	    	logService.add(opLog);
		}
        return message;
    }
    /**
     * 是否可以押宝，1、每天上午9:00—11:00  
     * 2、1次机会
     * @param empId 当前操作人
     * @return
     */
	private Message isCanBetting(String empId) {
		Calendar  c=Calendar.getInstance();
		int hour=c.get(Calendar.HOUR_OF_DAY);
		//获取是否要有时间限制，方便测试。false:没有限制 true:有限制
		String isLimit=Config.getProperty("guessKing.isLimit");
		if("false".equals(isLimit))
			log.debug("现在的押宝模式是无时间限制模式");
		else
			log.debug("现在的押宝模式是9-11点限制模式");
		//校验活动时间
		if( (hour>=9 && hour<=11 ) || "false".equals(isLimit)){
			//校验游戏机会
			List<Betting> list=bittingService.findBettingList(empId, new Date(), new Date());
			if(list!=null && list.size()>=1){
				return new Message(Message.STATE_one,"押宝机会已用完，请明天再来吧！");
			}else{
				return new Message(Message.STATE_200);
			}
		}else{
			return new Message(Message.STATE_one,"该游戏时间已过，请明天再来吧！");
		}
		
		
	}


	public static void main(String[] sdf){
//		System.out.println("sadfd".substring(0, 1));
//		
//		int[] data = new int[]{1,4,4,2,4,4,3,4,4}; //返回的数组
//		for(int i=0;i<10;i++){
//		int val = data[(int) Math.floor(Math.random()*data.length)];
//		System.out.println(val);
//		}
		Calendar  c=Calendar.getInstance();
		c.get(Calendar.AM);
		System.out.println(c.get(Calendar.DAY_OF_WEEK));
		c.add(Calendar.DAY_OF_WEEK, -3);
		System.out.println(c.get(Calendar.DAY_OF_WEEK));
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
	}
   
}
